<?php
include_once ('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'skpd_simpan') {
    $id_skpd = $_POST['id_skpd'];
    $idbid = $_POST['id_subbidang'];
    $idkla = $_POST['id_klasifikasi'];
    $nmskpd = $_POST['nama_skpd'];
    $kdskpd = $_POST['kode_skpd'];

    if (!is_numeric($id_skpd) || !is_numeric($idbid) || !is_numeric($idkla)) {
        echo "Err : invalid id. ";
        exit;
    }

    if ($id_skpd > 0) {
        exec_query("update mus_skpd set id_subbidangbappeda=" . $idbid . ", id_klasifikasi=" . $idkla . ", nama_skpd='" . $nmskpd . "', kode_skpd='" . $kdskpd . "' where id_skpd=" . $id_skpd . "");
		exec_query("update mus_akun set namaakun='".$nmskpd."',pass='".$nmskpd."',nama_pj='".$nmskpd."',nip_akun='".$nmskpd."' where id_skpd=".$id_skpd."");
		logs('Mengubah master SKPD','Nama SKPD : '.$nmskpd.'; Kode SKPD : '.$kdskpd.'; Nama Akun : '.$nmskpd.';');
		echo 'success__';
    } else {
        $maxid = get_maxid('id_skpd', 'mus_skpd');
        exec_query("insert into mus_skpd (id_skpd, kode_skpd, nama_skpd, id_subbidangbappeda, id_klasifikasi) values(" . $maxid . ",'" . $kdskpd . "', '" . $nmskpd . "', " . $idbid . ", " . $idkla . ")");
        $st = get_data('select id_skpd from mus_skpd where id_skpd=' . $maxid);
        if ($st['id_skpd'] == $maxid) {
            echo 'success__';
            $id_skpd = $maxid;
			
			logs('Menambah master SKPD','Nama SKPD : '.$nmskpd.'; Kode SKPD : '.$kdskpd.'; Nama Akun : '.$nmskpd.';');
        }
		$maxid1 = get_maxid('id_akun','mus_akun');
		exec_query("insert into mus_akun(id_akun, id_uslevel, id_skpd, namaakun, pass, nama_pj) values(".$maxid1.",4, ".$maxid.",'".$nmskpd."','".$kdskpd."','".$nmskpd."')");
		$tt = get_data('select id_akun from mus_akun where id_akun='.$maxid1);
		if($tt['id_akun']==$maxid1){
			$id_akun = $maxid1;
		}
    }
    echo $id_skpd . '__';
    view_skpd();
} else if ($act == 'hapus_skpd') {
    $id_skpd = $_POST['id_skpd'];
    if (is_numeric($id_skpd)) {
		$skpc = get_data('select kode_skpd,nama_skpd from mus_skpd where id_skpd='.$id_skpd);
        exec_query("delete from mus_skpd where id_skpd=" . $id_skpd);
		exec_query("delete from mus_akun where id_skpd=".$id_skpd);
        echo 'success__';
		logs('Menghapus master SKPD','Nama SKPD : '.$skpc['kode_skpd'].'; Kode SKPD : '.$skpc['nama_skpd'].';');
    } else {
        echo 'error';
    }
    view_skpd();
} else if ($act == 'ubah_skpd') {
    $id_skpd = $_POST['id_skpd'];
    if (is_numeric($id_skpd)) {
        $data = get_data("select id_skpd, id_subbidangbappeda, id_klasifikasi, kode_skpd, nama_skpd from mus_skpd where id_skpd=" . $id_skpd);
        $ress = implode($data, '__');
        print_r($ress);
        echo "__";
    } else {
        echo 'error';
    }

    view_skpd();
}

function view_skpd() {
    $no = 1;
    $kla = exec_query("select * from mus_klasifikasi order by id_klasifikasi");
    while ($rw1 = pg_fetch_array($kla)) {
        $id1 = $rw1['id_klasifikasi'];
        ?><tr>
            <td><?php echo $id1 ?></td>
            <td><b><?php echo $rw1['nama_klasifikasi'] ?></b></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <?php
        $skpd = exec_query("select id_skpd, kode_skpd, nama_skpd, nama_subbidang from mus_skpd s, mus_subbidangbappeda b where id_klasifikasi = " . $rw1['id_klasifikasi'] . " AND s.id_subbidangbappeda=b.id_subbidangbappeda order by id_skpd");
        while ($rw2 = pg_fetch_array($skpd)) {
            ?><tr>
                <td><?php echo $rw2['kode_skpd'] ?></td>
                <td><?php echo $rw2['nama_skpd'] ?></td>
                <td><?php echo $rw2['nama_subbidang'] ?></td>
                <td>
                  <a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit"  name="<?php echo $rw2['id_skpd'] ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                   <a class='btn btn-danger btn-small bt-hapus' name="<?php echo $rw2['id_skpd'] ?>"><i class="icon-trash icon-white"></i>Hapus</a>   
                </td>
            </tr>
            <?php
            $no++;
        }

        //$no++;
    }
}
?>

